package com.shameit.goodqrinfomanager.ui.activity.sync;

import android.app.Application;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.shameit.goodqrinfomanager.MyApplication;
import com.shameit.goodqrinfomanager.bean.litepal.CangKu;
import com.shameit.goodqrinfomanager.bean.litepal.CangWei;
import com.shameit.goodqrinfomanager.bean.litepal.ChanPin;
import com.shameit.goodqrinfomanager.bean.litepal.JingXiaoShang;
import com.shameit.goodqrinfomanager.bean.litepal.LoginUser;
import com.shameit.goodqrinfomanager.source.http.DataRepository;
import com.shameit.goodqrinfomanager.source.http.RequestBodyFactory;
import com.shameit.goodqrinfomanager.ui.activity.main.MainActivity;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class DataSyncViewModel extends BaseViewModel<DataRepository> {
    public DataSyncViewModel(@NonNull Application application) {
        super(application);
    }

    public DataSyncViewModel(@NonNull Application application, DataRepository model) {
        super(application, model);
    }

    public BindingCommand syncCangKu = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            ToastUtils.showShort("仓库同步");
            syncCk();
        }
    });

    public BindingCommand gotoCk = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bundle = new Bundle();
            bundle.putInt("type",0);
            startActivity(SyncLogActivity.class,bundle);
        }
    });

    public BindingCommand gotoCw = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bundle = new Bundle();
            bundle.putInt("type",1);
            startActivity(SyncLogActivity.class,bundle);
        }
    });

    public BindingCommand gotoCp = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bundle = new Bundle();
            bundle.putInt("type",2);
            startActivity(SyncLogActivity.class,bundle);
        }
    });

    public BindingCommand gotoJxs = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Bundle bundle = new Bundle();
            bundle.putInt("type",3);
            startActivity(SyncLogActivity.class,bundle);
        }
    });

    public BindingCommand syncCangwei = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            ToastUtils.showShort("仓位同步");
            syncCw();
        }
    });


    public BindingCommand syncChanpin = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            ToastUtils.showShort("产品同步");
            syncChanpin();
        }
    });

    public BindingCommand syncJxs = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            ToastUtils.showShort("经销商同步");
            setSyncJxs();
        }
    });

    public BindingCommand clear = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            LitePal.deleteDatabase("WarehouseManagement");
        }
    });

    public void syncCk(){
        HashMap hashMap = new HashMap();
        hashMap.put("token", MyApplication.TOKEN_VALUE);
        addSubscribe(model.syncCangku(RequestBodyFactory.getRequestBody(new Gson().toJson(hashMap)))
                .compose(RxUtils.Obs_io_main())
                .compose(RxUtils.handle_result())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        showDialog();
                    }
                }).subscribe(new Consumer<List<CangKu>>() {
                    @Override
                    public void accept(List<CangKu> cangKus) throws Exception {
                        for (CangKu ck: cangKus
                             ) {
                            ck.save();
                        }
                        dismissDialog();
                        ToastUtils.showShort("同步成功");
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        dismissDialog();
                        ToastUtils.showShort("失败!错误码：" + throwable.getMessage());
                    }
                })
        );
    }

    public void syncCw(){
        addSubscribe(model.syncCangwei()
                .compose(RxUtils.Obs_io_main())
                .compose(RxUtils.handle_result())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        showDialog();
                    }
                }).subscribe(new Consumer<List<CangWei>>() {
                    @Override
                    public void accept(List<CangWei> cangKus) throws Exception {
                        for (CangWei ck: cangKus
                        ) {
                            ck.save();
                        }
                        dismissDialog();
                        ToastUtils.showShort("同步成功");
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        dismissDialog();
                        ToastUtils.showShort("失败!错误码：" + throwable.getMessage());
                    }
                })
        );
    }

    public void syncJxs(){
        addSubscribe(model.syncJxs()
                .compose(RxUtils.Obs_io_main())
                .compose(RxUtils.handle_result())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        showDialog();
                    }
                }).subscribe(new Consumer<List<JingXiaoShang>>() {
                    @Override
                    public void accept(List<JingXiaoShang> cangKus) throws Exception {
                        for (JingXiaoShang ck: cangKus
                        ) {
                            ck.save();
                        }
                        dismissDialog();
                        ToastUtils.showShort("同步成功");
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        dismissDialog();
                        ToastUtils.showShort("失败!错误码：" + throwable.getMessage());
                    }
                })
        );
    }

    public void setSyncJxs(){
        addSubscribe(
                model.syncJxs()
                .compose(RxUtils.Obs_io_main())
                .compose(RxUtils.handle_result())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        showDialog();
                    }
                }).subscribe(new Consumer<List<JingXiaoShang>>() {
                     @Override
                     public void accept(List<JingXiaoShang> jingXiaoShangs) throws Exception {
                         for (JingXiaoShang bean : jingXiaoShangs) {
                             bean.save();
                         }
                         dismissDialog();
                         ToastUtils.showShort("同步成功");
                     }
                 }, new Consumer<Throwable>() {
                     @Override
                     public void accept(Throwable throwable) throws Exception {
                         dismissDialog();
                         ToastUtils.showShort("失败!错误码：" + throwable.getMessage());
                     }
                 }
                )
        );
    }

    public void syncChanpin(){
        addSubscribe(model.syncChanpin()
                .compose(RxUtils.Obs_io_main())
                .compose(RxUtils.handle_result())
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
                        showDialog();
                    }
                }).subscribe(new Consumer<List<ChanPin>>() {
                    @Override
                    public void accept(List<ChanPin> cangKus) throws Exception {
                        for (ChanPin ck: cangKus
                        ) {
                            ck.save();
                        }
                        dismissDialog();
                        ToastUtils.showShort("同步成功");
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        dismissDialog();
                        ToastUtils.showShort("失败!错误码：" + throwable.getMessage());
                    }
                })
        );
    }
}
